package Homework1;

/**
 *
 * @author Jakim
 */
import java.util.*;

class AlreadyInTheMapException extends Exception {

    String mErrorMessage;

    public AlreadyInTheMapException() {
        super();
        mErrorMessage = "Already exists in!";
    }

    public AlreadyInTheMapException(String aErrorMessage) {
        super(aErrorMessage);
        mErrorMessage = aErrorMessage;
    }

    public String getmErrorMessage() {
        return mErrorMessage;
    }
}

class NotInTheMapException extends Exception {

    String mErrorMessage;

    public NotInTheMapException() {
        super();
        mErrorMessage = "Not such student!";
    }

    public NotInTheMapException(String aErrorMessage) {
        super(aErrorMessage);
        mErrorMessage = aErrorMessage;
    }

    public String getmErrorMessage() {
        return mErrorMessage;
    }
}

public class Course {

    private boolean isSeparated = false;
    private ArrayList<Student> mStudentList = new ArrayList<Student>(10);
    private ArrayList<Student> mSmartStudentsList = new ArrayList<Student>(10);
    private ArrayList<Student> mLameStudentsList = new ArrayList<Student>(10);
    private HashMap<String, Student> mStudentsMap;
    private Student mStudent;
    
    public Course() {
        super();
        mStudentsMap = new HashMap<String, Student>();
    }

    public void addStudent(Student aStudent) {
        mStudentList.add(aStudent);
    }

    public Student addStudent(String aName, String aFacutlyNumber) throws AlreadyInTheMapException {

        if (this.mStudentsMap.containsKey(aFacutlyNumber)) {
            throw new AlreadyInTheMapException(" A student with faculty nummber "
                    + aFacutlyNumber + " already exists");
        }

        Student tmpStudent = new Student(aName);
        tmpStudent.setFacultyNumber(aFacutlyNumber);
        this.mStudentsMap.put(aFacutlyNumber, tmpStudent);
        return tmpStudent;
    }

    public void SeparateSudents() {
        Student tmpStudent;

        if (this.isSeparated == true) {
            System.out.println("Already has been separated.");
        }

        if (this.mStudentsMap.size() == 0) {
            return;
        } else {
            this.mStudentList.clear();
            this.mLameStudentsList.clear();
            this.mSmartStudentsList.clear();
            this.mStudentList.addAll(this.mStudentsMap.values());
        }

        Iterator studentIterator = this.mStudentList.iterator();
        while (studentIterator.hasNext()){
            tmpStudent =(Student) studentIterator.next();
            if (tmpStudent.getArithmeticalMean() >= 4){
                this.mSmartStudentsList.add(tmpStudent);
            }else{
                this.mLameStudentsList.add(tmpStudent);
            }
        }
        this.mStudentList.removeAll(this.mLameStudentsList);
        this.isSeparated = true;
    }

    public void printAllSmartStudents(){

        Iterator studentIterator = this.mSmartStudentsList.iterator();
        while (studentIterator.hasNext()){
            System.out.println(studentIterator.next());
        }
    }

    public void printAllLameStudents(){
        Iterator studentIterator = this.mLameStudentsList.iterator();
        while (studentIterator.hasNext()){
            System.out.println(studentIterator.next());
        }
    }

    public Student getStudent(String aFacutlyNumber) throws NotInTheMapException {

        Student tmpStudent;
        tmpStudent = this.mStudentsMap.get(aFacutlyNumber);

        if (tmpStudent == null) {
            throw new NotInTheMapException(" A student with faculty nummber "
                    + aFacutlyNumber + " does not exist");
        } else {
            return tmpStudent;
        }
    }

    public HashMap<String, Student> getAllSmartStudents() {

        return null;
    }

    public Student getStudent() {
        mStudent = (Student) mStudentList.get(0);
        return mStudent;
    }
}
